home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / netlog.c < prev    next >
C/C++ Source or Header  |  1991-04-26  |  2KB  |  111 lines

  1. /* @(#) $Header: netlog.c,v 1.4 91/04/25 18:27:17 deyke Exp $ */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/rtprio.h>
  6. #include <unistd.h>
  7.  
  8. #include "global.h"
  9.  
  10. #define LOGFILEDIR "/tcp/logs"
  11. #define START_REC  1
  12.  
  13. static FILE *fplog;
  14. static FILE *fps[_NFILE];
  15. static int  lastchr[_NFILE];
  16.  
  17. static void netlog __ARGS((void));
  18. static void start_netlog __ARGS((void));
  19.  
  20. /*---------------------------------------------------------------------------*/
  21.  
  22. static void netlog()
  23. {
  24.  
  25.   char  filename[80];
  26.   int  chr;
  27.   int  i;
  28.   int  s;
  29.   static int  cnt;
  30.  
  31.   for (; ; ) {
  32.     while (getc(fplog) != START_REC)
  33.       if (ferror(fplog) || feof(fplog)) return;
  34.     i = getc(fplog);
  35.     if (i < 0 || i >= _NFILE) continue;
  36.     s = getw(fplog);
  37.     if (s > 0) {
  38.       if (!fps[i]) {
  39.     sprintf(filename, "%s/log.%05d.%04d", LOGFILEDIR, getpid(), cnt++);
  40.     fps[i] = fopen(filename, "a");
  41.       }
  42.       if (!fps[i]) {
  43.     while (s--) chr = getc(fplog);
  44.       } else {
  45.     while (s--) {
  46.       chr = getc(fplog);
  47.       if (!chr) {
  48.         /* ignore nulls */
  49.       } else if (chr == '\r')
  50.         putc('\n', fps[i]);
  51.       else if (chr != '\n' || lastchr[i] != '\r')
  52.         putc(chr, fps[i]);
  53.       lastchr[i] = chr;
  54.     }
  55.     fflush(fps[i]);
  56.       }
  57.     } else if (s < 0 && fps[i]) {
  58.       fclose(fps[i]);
  59.       fps[i] = 0;
  60.       lastchr[i] = 0;
  61.     }
  62.   }
  63. }
  64.  
  65. /*---------------------------------------------------------------------------*/
  66.  
  67. static void start_netlog()
  68. {
  69.   int  fd[2], i;
  70.  
  71.   if (pipe(fd)) return;
  72.   switch (fork()) {
  73.   case -1:
  74.     close(fd[0]);
  75.     close(fd[1]);
  76.     break;
  77.   case 0:
  78.     rtprio(0, RTPRIO_RTOFF);
  79.     for (i = 0; i < _NFILE; i++)
  80.       if (i != fd[0]) close(i);
  81.     fplog = fdopen(fd[0], "r");
  82.     netlog();
  83.     exit(0);
  84.     break;
  85.   default:
  86.     close(fd[0]);
  87.     fplog = fdopen(fd[1], "w");
  88.     break;
  89.   }
  90. }
  91.  
  92. /*---------------------------------------------------------------------------*/
  93.  
  94. void write_log(fd, buf, cnt)
  95. int  fd;
  96. char  *buf;
  97. int  cnt;
  98. {
  99.   if (!cnt) return;
  100.   if (!fplog) {
  101.     start_netlog();
  102.     if (!fplog) return;
  103.   }
  104.   putc(START_REC, fplog);
  105.   putc(fd, fplog);
  106.   putw(cnt, fplog);
  107.   while (--cnt >= 0) putc(*buf++, fplog);
  108.   fflush(fplog);
  109. }
  110.  
  111.